using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Script.Services;
using System.Web.Security;
using System.Web.Services;
using System.ComponentModel;
using Dzzzr;

namespace NWS
{
  /// <summary>
  /// Summary description for AJAXService
  /// </summary>
  [WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  [ToolboxItem(false)]
  [ScriptService]
  public class AJAXService : WebService
  {
    /// <summary>
    /// Initialize remote service
    /// </summary>
    /// <returns></returns>
    //private static DzzzrPointingService InitRemoteService()
    //{
    //  DzzzrPointingService service = new DzzzrPointingService();
    //  service.Url = ConfigurationManager.AppSettings["PointingService"];
    //  return service;
    //}

    /// <summary>
    /// Add point from Marker
    /// </summary>
    /// <param name="markerName">Marker name</param>
    /// <param name="latitude">Point latitude</param>
    /// <param name="longitude">Point logitude</param>
    //[WebMethod(true)]
    //public void AddPoint(string markerName, float latitude, float longitude)
    //{
    //  Entity entity =  Entity.GetEntityByName(Helper.TeamID, markerName);
    //  Marker.AddPoint(Membership.GetUser().UserName, entity.ID, Helper.CurrentGame.Name, latitude, longitude, 0, -1);
    //}

    /// <summary>
    /// Add point from Marker
    /// </summary>
    [WebMethod(true)]
    public void AddPoint(int entityID, float latitude, float longitude)
    {
      Marker.AddPoint(entityID, Helper.CurrentGame.ID, (int) Membership.GetUser().ProviderUserKey, latitude, longitude, 0, -1);
    }
    ///// <summary>
    ///// Get last markers positions by game name
    ///// </summary>
    ///// <returns>Array of markers</returns>
    //[WebMethod(true)]
    //public SimpleMarker[] GetEntityList()
    //{
    //  try
    //  {
    //    string gameName = (string)Session[Constants.CurrentGame];
    //    DzzzrPointingService service = InitRemoteService();
    //    return service.GetEntityList(gameName, Google.GetLogin(), Google.GetPassword());

    //  }
    //  catch
    //  {
    //    return new SimpleMarker[] { };

    //  }
    //}

    ///// <summary>
    ///// Get root entities in game 
    ///// </summary>
    ///// <returns>Array of markers</returns>
    //[WebMethod(true)]
    //public Entity[] GetEntityList(int gameID)
    //{
    //  try
    //  {
    //    MonitoringClient monitoringClient = new MonitoringClient();
    //    Entity[] entities = monitoringClient.GetGameEntities(Helper.UserName, Helper.Password, gameID);

    //    //return service.GetEntityList(gameName, Google.GetLogin(), Google.GetPassword());
    //    return entities;

    //  }
    //  catch
    //  {
    //    return new Entity[] { };

    //  }
    //}

    /// <summary>
    /// Get last position of entities in game 
    /// </summary>
    /// <returns>Array of markers</returns>
    [WebMethod(true)]
    public List<Marker> GetPoints(int gameID)
    {
      try
      {
        return Marker.GetMarkerList(gameID);
      }
      catch(Exception /*ex*/)
      {
        List<Marker> exc = new List<Marker>();
        exc.Add(new Marker(-1, "Exception", 54, 39, DateTime.Now, 0.0, 0.0, 0));
        return exc; 
      }
    }

    /// <summary>
    /// Returns verbose HTML description of marker
    /// </summary>
    /// <param name="markerID"></param>
    /// <returns></returns>
    [WebMethod(true)]
    public string GetMarkerDescription(int markerID)
    {
      MarkerInfo mi = MarkerInfo.GetMarkerInfo(Helper.CurrentGame.ID, markerID);
      //MonitoringClient monitoringClient = new MonitoringClient();
      //Entity[] entities = monitoringClient.GetSubEntities(Helper.UserName, Helper.Password, markerID);
      //string html = Helper.CreateInfoHtml(entities);
      //return html;
      if (mi != null)
        return mi.GetInfoString();
      else
        return String.Empty;
    }

  }
}
